load("@fbcode//eden/fs:build_targets.bzl", "make_fbpkg_path_actions")
load("@fbcode//fbpkg:fbpkg.bzl", "fbpkg")
load("@fbcode//registry:defs.bzl", "rpm")
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule")

oncall("scm_client_infra")

# The :eden rule is a convenience to ensure that the EdenFS CLI, daemon, and SCM
# integration are built.
buck_genrule(
    name = "eden",
    out = "eden-stamp",
    cmd = "echo $(location //eden/fs/cli:edenfsctl) " +
          "$(location //eden/fs/service:edenfs) " +
          "$(location //eden/fs/service:edenfs_privhelper) " +
          "> $OUT",
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
)

# TODO: T246598314: Unify with rpm.builder in eden/fs
fbpkg.builder(
    name = "scm.edenfs",
    architectures = [
        "x86_64",
        "aarch64",
    ],
    buck_opts = fbpkg.buck_opts(
        mode = "opt",
    ),

    # This is doing (almost) the same as eden/fs/facebook/packaging/packman.yml
    # For the config we cannot do with fbpkg.builder (e.g. set mode for a binary) we
    # should rely on Chef or an installation script to config correctly.
    #
    # TODO:
    #  - expiry
    #  - mode: edenfs_privhelper : 04755
    #  - dirs
    #    - path: /etc/eden
    #      mode: '0755'
    #    - path: /etc/eden/config.d
    #      mode: '0755'
    # We rely on the install script install.toml/sandcastle_install.sh to install the contents in the correct location
    # Paths under bin/ should be put into "/usr/local/bin/"
    # Paths under libexec/ should be put into "/usr/local/libexec/"
    # Paths under etc/ should be put into "/etc"
    # Paths under locale/ should be put into "/usr/share/locale/"
    path_actions = make_fbpkg_path_actions(),
    split_debuginfo = True,
)

rpm.builder(
    name = "fb-eden-locale",
    autoclean_keep_versions = 200,
    configurations = [
        rpm.configuration(
            arch = "x86_64",
        ),
        rpm.configuration(
            arch = "aarch64",
        ),
    ],
    features = [
        rpm.install(
            src = "locale/glibc_en.mo",
            dst = "/usr/share/locale/en/LC_MESSAGES/libc.mo",
            mode = 0o0755,
        ),
    ],
    strip_debuginfo_without_saving_symbols = True,
    summary = "Customized errno message translations for Eden.",
)
